package com.digital.smartgrow.controller.expert;

import com.digital.smartgrow.common.advice.Result;
import com.digital.smartgrow.dto.expert.ExpertPageDTO;
import com.digital.smartgrow.entity.expert.Expert;
import com.digital.smartgrow.service.expert.ExpertService;
import com.digital.smartgrow.service.expert.random.RandomExpertGenerate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 教师基本信息 前端控制器
 * </p>
 *
 * @author sunyuechao
 * @since 2022-07-16
 */
@Api(tags = "专家接口")
@RestController
@RequestMapping("${context.prefix}/business/expert")
public class ExpertController {

    @Autowired
    private ExpertService expertService;
    @Autowired
    private RandomExpertGenerate expertGenerate;

    //@ApiOperation(value = "抽选专家")
    //@PostMapping("/generate")
    public Object generate(@RequestBody  @ApiParam(value = "被抽选专家")  List<Expert> expertList,  @ApiParam(value = "抽选数量") Integer count){

        List<Expert> experts = null;
        try {
            experts = expertGenerate.generateAll(expertList, count);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.success(experts);
    }

    @ApiOperation(value = "专家信息-分页查询")
    @PostMapping("/list")
    public Object getListByPage(@RequestBody ExpertPageDTO expertPageDTO){
        return expertService.getListByPage(expertPageDTO);
    }

    @ApiOperation(value = "专家信息-详情")
    @GetMapping("/{id}")
    public Object getInfo(@PathVariable Integer id){
        if (id == null) return Result.fail("id不能为空");
        return expertService.getInfo(id);
    }

    @ApiOperation(value = "专家信息-删除")
    @DeleteMapping("/{id}")
    public Object removeById(@PathVariable Integer id){
      if (id == null) return Result.fail("id不能为空");
      return expertService.removeById(id);
    }
    @ApiOperation(value = "专家信息-新增")
    @PostMapping()
    public Object add( @RequestBody Expert expert){
       return expertService.save(expert);
    }

    @ApiOperation(value = "专家信息-编辑")
    @PutMapping()
    public Object edit(@RequestBody Expert expert){
    if (expert==null||expert.getId() == null)
       return Result.fail("id不能为空");
       return expertService.updateById(expert);
    }

    @ApiOperation(value = "专家审批-列表")
    @GetMapping("/approve")
    public Object approveList(){
        return expertService.approveList();
    }
}
